package com.pzhuweb.sgblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhuweb.sgblog.domain.dto.LinkDto;
import com.pzhuweb.sgblog.domain.entity.Link;
import com.pzhuweb.sgblog.domain.enums.AppHttpCodeEnum;
import com.pzhuweb.sgblog.domain.vo.PageVo;
import com.pzhuweb.sgblog.exception.SystemException;
import com.pzhuweb.sgblog.mapper.LinkMapper;
import com.pzhuweb.sgblog.service.LinkService;
import com.pzhuweb.sgblog.domain.utils.BeanCopyUtils;
import com.pzhuweb.sgblog.domain.utils.ResponseResult;
import com.pzhuweb.sgblog.domain.utils.SystemConstants;
import com.pzhuweb.sgblog.domain.vo.LinkVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {
    @Override
    public ResponseResult<?> getAllLink() {
        // 查询所有通过审核的友链
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> linkList = list(queryWrapper);
        // 封装vo
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(linkList, LinkVo.class);
        return ResponseResult.okResult(linkVos);

    }

    @Override
    public ResponseResult<?> getPage(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), Link::getName, name)
                .eq(StringUtils.hasText(status), Link::getStatus, status);
        Page<Link> linkPage = new Page<>(pageNum, pageSize);
        page(linkPage, queryWrapper);
        PageVo pageVo = new PageVo(linkPage.getRecords(), linkPage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult<?> addLink(LinkDto linkDto) {
        if (!StringUtils.hasText(linkDto.getName())) {
            throw new SystemException(AppHttpCodeEnum.LINK_NAME_ISNULL);
        }
        if (!StringUtils.hasText(linkDto.getDescription())) {
            throw new SystemException(AppHttpCodeEnum.LINK_DESCRIPTION_ISNULL);
        }
        if (!StringUtils.hasText(linkDto.getLogo())) {
            throw new SystemException(AppHttpCodeEnum.LINK_LOGO_ISNULL);
        }
        if (!StringUtils.hasText(linkDto.getAddress())) {
            throw new SystemException(AppHttpCodeEnum.LINK_ADDRESS_ISNULL);
        }
        Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
        save(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<?> getDetails(Long id) {
        Link link = getById(id);
        if (Objects.isNull(link)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);
        return ResponseResult.okResult(linkVo);
    }

    @Override
    public ResponseResult<?> updateLink(LinkDto linkDto) {
        LambdaUpdateWrapper<Link> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Link::getId,linkDto.getId())
                .set(Link::getName,linkDto.getName())
                .set(Link::getDescription,linkDto.getDescription())
                .set(Link::getLogo,linkDto.getLogo())
                .set(Link::getAddress,linkDto.getAddress())
                .set(Link::getStatus,linkDto.getStatus());
        update(updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<?> deleteLink(List<Long> ids) {
        removeBatchByIds(ids);
        return ResponseResult.okResult();
    }
}
